package cl.vixionarios.biotika.util.proxy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public abstract class Proxy {

	
	
	
	
	public static String doPut(String url, List<NameValuePair> qparams )
	{
		String json=null;

		 String par=qparams==null?"":"?"+URLEncodedUtils.format(qparams, "UTF-8");//ma;
		
		 String urlExterno=url+par;
		 
		 //String url ="http://localhost:8080/MinSalud/formJson.jsp"+par;
		
		 
		 

		 HttpClient httpclient = new  DefaultHttpClient();
		 try {
	            //HttpPost httppost = new HttpPost(url);
	            HttpPut httpPut = new HttpPut(urlExterno);

	            HttpResponse response = httpclient.execute(httpPut);
	            HttpEntity resEntity = response.getEntity();


	            
	            
	            if (response.getStatusLine().getStatusCode()==200 &&  resEntity != null) {
	            	
	            	json=page(resEntity.getContent());
	            
	            }
	           
	            EntityUtils.consume(resEntity);
	        } catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
	            try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
	        }
		 	
		 return json;
	}
	
	
	
	public static String doGet(String url, List<NameValuePair> qparams )
	{
		String json=null;

		 String par=qparams==null?"":"?"+URLEncodedUtils.format(qparams, "UTF-8");
		
		 String urlExterno=url+par;
		 
		 //String url ="http://localhost:8080/MinSalud/formJson.jsp"+par;
		
		 
		 

		 HttpClient httpclient = new  DefaultHttpClient();
		 try {
	            //HttpPost httppost = new HttpPost(url);
	            HttpGet httpGet = new HttpGet(urlExterno);

	            HttpResponse response = httpclient.execute(httpGet);
	            HttpEntity resEntity = response.getEntity();

	           
	    
	            
	            
	            if (response.getStatusLine().getStatusCode()==200 &&  resEntity != null) {
	            	
	            	json=page(resEntity.getContent());
	            
	            }
	            EntityUtils.consume(resEntity);
	        } catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
	            try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
	        }
		 	
		 return json;
	}
	
	
	
	public static String doPost(String url, List<NameValuePair> qparams )
	{
		String json=null;

	
		 
		 //String url ="http://localhost:8080/MinSalud/formJson.jsp"+par;
		
		 
		 

		 HttpClient httpclient = new  DefaultHttpClient();
		 try {
	            //HttpPost httppost = new HttpPost(url);
	            HttpPost httpPost = new HttpPost(url);

	            UrlEncodedFormEntity sa= new UrlEncodedFormEntity(qparams);
//			 	sa.setContentType("text/plain");
			   httpPost.setEntity(sa);
	            
	            HttpResponse response = httpclient.execute(httpPost);
	            HttpEntity resEntity = response.getEntity();


	            
	    
	            
	            
	            if (response.getStatusLine().getStatusCode()==200 &&  resEntity != null) {
	            	
	            	json=page(resEntity.getContent());
	            
	            }
	            EntityUtils.consume(resEntity);
	        } catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
	            try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
	        }
		 	
		 return json;
	}
	
	
	
	
	
	public static String doPost(String url, MultipartEntity entity  )
	{
		String json=null;

		 
		 
		 //String url ="http://localhost:8080/MinSalud/formJson.jsp"+par;
		
		 
		 

		 HttpClient httpclient = new  DefaultHttpClient();
		 try {
	            //HttpPost httppost = new HttpPost(url);
	            HttpPost httpPost = new HttpPost(url);

	            httpPost.setEntity(entity);	            
	            HttpResponse response = httpclient.execute(httpPost);
	            HttpEntity resEntity = response.getEntity();

	 
	            
	    
	            
	            
	            if (response.getStatusLine().getStatusCode()==200 &&  resEntity != null) {
	            	
	            	json=page(resEntity.getContent());
	            
	            }
	            EntityUtils.consume(resEntity);
	        } catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
	            try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
	        }
		 	
		 return json;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public static String doDelete(String url, List<NameValuePair> qparams )
	{
		String json=null;

		 String par=qparams==null?"":"?"+URLEncodedUtils.format(qparams, "UTF-8");
		
		 String urlExterno=url+par;

		 
		 

		 HttpClient httpclient = new  DefaultHttpClient();
		 try {
	            
	            HttpDelete httpDelete = new HttpDelete(urlExterno);

	            HttpResponse response = httpclient.execute(httpDelete);
	            HttpEntity resEntity = response.getEntity();

	            
	    
	            
	            
	            if (response.getStatusLine().getStatusCode()==200 &&  resEntity != null) {
	            	
	            	json=page(resEntity.getContent());
	            
	            }
	            EntityUtils.consume(resEntity);
	        } catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
	            try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
	        }
		 	
		 return json;
	}
	
	
	
	
	
	
	
	
	private static String page(InputStream dato)
	{
		String json=null;
		 BufferedReader in = null;
		 try {
		 in = new BufferedReader
 	            (new InputStreamReader(dato));
 	            StringBuffer sb = new StringBuffer("");
 	            String line = "";
 	           // String NL = System.getProperty("line.separator");
 	            while ((line = in.readLine()) != null) {
 	                sb.append(line);// + NL);
 	            }

					in.close();
					
					json= new String(sb.toString().getBytes(),"utf-8") ;
					
 	           } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
 	           } finally {
 	              if (in != null) {
 	                  try {
 	                      in.close();
 	                      } catch (IOException e) {
 	                      e.printStackTrace();
 	                  }
 	              }
 	            
 	           }
		 
		 	  //System.out.println(json);
		
 	          return (json);// = sb.toString();
 	            
 	            
 	       
	}
}
